<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <myHead th:replace="basic/layout :: head('用户注册')"></myHead>
</head>
<body>
<div class="container mt-5">
  <div class="row justify-content-center">
    <div class="col-md-6">
      <div class="card">
        <div class="card-header">
          <h3>用户注册</h3>
        </div>
        <div class="card-body">
          <!-- 消息提示 -->
          <div th:if="${msg}" class="alert alert-success">
            <span th:text="${msg}"></span>
          </div>
          <div th:if="${error}" class="alert alert-danger">
            <span th:text="${error}"></span>
          </div>
          
          <!-- 注册表单（绑定User实体） -->
          <form th:action="@{/doRegister}" method="post" th:object="${user}">
            <!-- 用户名 -->
            <div class="mb-3">
              <label class="form-label">用户名</label>
              <input type="text" th:field="*{username}" class="form-control" placeholder="请输入用户名">
            </div>
            
            <!-- 密码 -->
            <div class="mb-3">
              <label class="form-label">密码</label>
              <input type="password" th:field="*{password}" class="form-control" placeholder="请输入密码">
            </div>
            
            <!-- 邮箱 -->
            <div class="mb-3">
              <label class="form-label">邮箱</label>
              <input type="email" th:field="*{email}" class="form-control" placeholder="请输入邮箱">
            </div>
            
            <!-- 验证码 -->
            <div class="mb-3">
              <div class="input-group">
                <input type="text" name="verifyCode" class="form-control" placeholder="请输入验证码">
                <button type="button" id="sendCodeBtn" class="btn btn-secondary">获取验证码</button>
              </div>
            </div>
            
            <!-- 提交 -->
            <button type="submit" class="btn btn-primary">注册</button>
            <a th:href="@{/toLoginPage}" class="btn btn-link">已有账号？登录</a>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  // 获取CSRF令牌和头信息（从元标签中）
  const csrfToken = $("meta[name='_csrf']").attr("content");
  const csrfHeader = $("meta[name='_csrf_header']").attr("content");
  // 全局配置Ajax，自动携带CSRF令牌（所有POST请求生效）
  $(document).ajaxSend(function(event, xhr, options) {
    xhr.setRequestHeader(csrfHeader, csrfToken);
  });
  // 验证码发送逻辑
  $(function() {
    let countdown = 60;
    let isSending = false;
    
    $("#sendCodeBtn").click(function() {
      const email = $("input[name='email']").val();
      if (!email) {
        alert("请输入邮箱");
        return;
      }
      if (isSending) return;
      
      isSending = true;
      $(this).prop("disabled", true).text("发送中...");
      
      $.post("/sendCode", { email: email }, function(result) {
        if (result === "success") {
          alert("验证码已发送");
          let timer = setInterval(function() {
            countdown--;
            $("#sendCodeBtn").text(countdown + "秒后重发");
            if (countdown <= 0) {
              clearInterval(timer);
              $("#sendCodeBtn").prop("disabled", false).text("获取验证码");
              countdown = 60;
              isSending = false;
            }
          }, 1000);
        } else {
          alert(result);
          $("#sendCodeBtn").prop("disabled", false).text("获取验证码");
          isSending = false;
        }
      });
    });
  });
</script>
</body>
</html>
